﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Framework.Library.Base.Base;
using Framework.Library.Data.DAL;
using Framework.Library.Data.DAO;
using Framework.Library.Core.Utils;
using Framework.Library.Core.Utils.Text;
using Framework.Library.Data.DAL.JObject.bgtt;
using Framework.Library.Core.Utils.Json;
using Framework.Library.Core.Utils.URLBuilder;

namespace Framework.Portal.Layouts.Admin.Components.BGTT.Courses
{
    public partial class CoursesAdd : BaseControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadCategories();
                GetActiveTab();
                LoadData();
            }
        }

        private void LoadCategories()
        {
            List<BGTTCategories> lst = ManagerFactory.DAOEntity.GetList(new BGTTCategories());
            if (lst != null)
            {
                var lstCategories = lst.FindAll(x => x.TYPE == 2);
                if (lstCategories.Count > 0)
                {
                    drpCategories.Items.Clear();
                    foreach (var info in lstCategories)
                    {
                        drpCategories.Items.Add(new ListItem(info.NAME, info.ID.ToString()));
                    }
                }
                var lstYears = lst.FindAll(x => x.TYPE == 1);
                if (lstYears.Count > 0)
                {
                    drpYear.Items.Clear();
                    foreach (var info in lstYears)
                    {
                        drpYear.Items.Add(new ListItem(info.NAME, info.ID.ToString()));
                    }
                }
            }
        }
        private void GetActiveTab()
        {
            int courseId;
            int.TryParse(Request.QueryString["c"], out courseId);
            if (courseId == 0)
            {
                if (!string.IsNullOrEmpty(lblID.Text))
                    courseId = Convert.ToInt32(lblID.Text);
            }
            if (courseId > 0)
            {
                btn1.Enabled = true;
                btn2.Enabled = true;
                btn3.Enabled = true;
                btn4.Enabled = true;
                btn5.Enabled = true;
                btn6.Enabled = true;
            }
        }
        protected void ActiveTab(object sender, EventArgs e)
        {
            var sends = sender as LinkButton;
            if (sends != null)               
            {
                var urlParser = new SimpleURLParameterParser(Request.RawUrl);
                var lstUrl = new Dictionary<string, string>();
                lstUrl.Add("t", sends.CommandArgument);
                if (Convert.ToInt32(sends.CommandArgument) > 0)
                    lstUrl.Add("c", lblID.Text);
                Response.Redirect(urlParser.CreateQueryString(lstUrl));
            }
        }

        protected void cmdAddNew_Click(object sender, EventArgs e)
        {
            var info = GetData();
            var result = ManagerFactory.DAOEntity.Insert(info);
            if (result > 0)
            {
                lblID.Text = result.ToString();
                GetActiveTab();
                lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
            }
            else
                lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_ERROR");
        }

        protected void cmdEmpty_Click(object sender, EventArgs e)
        {

        }

        protected void cmdDelete_Click(object sender, EventArgs e)
        {

        }
        protected void cmdUpdate_Click(object sender, EventArgs e)
        {

        }
        private void LoadData()
        {
            int courseId;
            int.TryParse(Request.QueryString["c"], out courseId);
            if (courseId > 0)
            {
                lblID.Text = courseId.ToString();
            }
        }
        private BGTTCourses GetData()
        {
            BGTTCourses info = new BGTTCourses();
            info.ID = 0;
            info.TITLE = txtTitle.Text;
            info.SHORTTILE = txtShortTitle.Text;
            info.STARTTIME = ConvertUtility.ToUnixTime(Convert.ToDateTime(txtStartDate.Text));
            info.CATEGORYID = Convert.ToInt32(drpCategories.SelectedValue);
            info.GRADEID = Convert.ToInt32(drpYear.SelectedValue);
            info.AVATAR = txtImage.Text;
            info.DESCRIPTION = StringUtils.RemoveHtml(txtDescription.Text);
            info.BACKGROUND = StringUtils.RemoveHtml(txtBackground.Text);
            info.RESULT = StringUtils.RemoveHtml(txtResult.Text);
            int seat;
            int.TryParse(txtSeat.Text, out seat);
            info.TOTALSEATS = seat;
            info.STATUS = 1;
            List<JSchedules> lstSchedule = new List<JSchedules>();
            info.SCHEDULES = JsonHelper.ToJSON(lstSchedule);
            List<JAnnouncement> lstAnnounce = new List<JAnnouncement>();
            info.ANNOUNCEMENTS = JsonHelper.ToJSON(lstAnnounce);
            info.FOLLOWERS = string.Empty;
            info.STUDENTS = string.Empty;
            List<JFAQ> lstFAQ = new List<JFAQ>();
            info.FAQ = JsonHelper.ToJSON(lstFAQ);
            return info;
        }
    }
}